﻿(function () {
    "use strict";
    var e = window.jQuery,
		t = window.qh360cp,
		n = window._;
    t.pages = t.pages || {}, t.pages.init = function () {
        e("input[name=itext]").on("keyup", function () {
            var t = e(this).val().replace(/[^(\.|\d)]/g, "");
            e(this).val(t)
        }),
        e("input[name=itext]").on("keyup", function () {
            var n = t.pages.toNumber(e(this).val());
            e(this).val(n);
            var r = e("#itotal").val() * 1,
				i = e("#iafter").val() * 1,
				s = e("#itown").val() * 1;
            r < s && (s = r, e("#itown").val(r));
            if (r) {
                var o = t.number.percent(s / r, 100, 3) * 1;
                e(".perown").text(o + "%")
            }
        }),
        e("input[name=fan]").on("change", function () {
            t.pages.primaryhm()
        }),
        e("#calcul").on("click", function () {
            t.pages.primaryhm()
        })
    },





    t.pages.toNumber = function (t) {
        return e.isNumeric(t) ? t * 1 : t.split(".").slice(0, 2).join(".") * 1
    },
    t.pages.primaryhm = function () {
        var r = e("#per").val() * 1,
			i = e("#iafter").val() * 1,
			s = e("#itotal").val() * 1,
			o = e("#itown").val() * 1,
			u = 0,
			a = 0,
			f = t.number.percent(o / s, 100, 3) * 1;
        if (s > 0) if (i > s) {
            if (e("#fq").prop("checked")) {
                e(".templete li").eq(0).show().end().eq(1).hide().end().eq(2).hide(), a = i * o / s + (i - s) * r / 100 * (1 - o / s), u = t.number.format(a, 2, -1) * 1, u = t.number.currency(u, 2, 0);
                var l = {
                    per: r,
                    bonus: t.number.currency(i, 0, 0),
                    total: t.number.currency(s, 0, 0),
                    own: t.number.currency(o, 0, 0),
                    sub: f,
                    mybonus: u
                },
					c = '<td>\u3010</td><td><%=bonus%></td><td>\uff0d\uff08</td><td><%=bonus%></td><td>\uff0d</td><td><%=total%></td><td>\uff09\u00d7</td><td><%=per%>%</td><td>\u3011\u00d7</td><td><%=sub%>%</td><td>+\uff08</td><td><%=bonus%></td><td>\uff0d</td><td><%=total%></td><td>\uff09\u00d7</td><td><%=per%>%</td><td>=</td><td><em class="red" style="font-weight:bold"><%=mybonus%></em> \u5143</td>',
					h = n.template(c);
                e(".templete li").eq(0).find(".tmp1").html(h(l)), e(".fsz20").html(u)
            } else {
                a = i * (1 - r / 100) * o / s + s * r / 100 * o / s, u = t.number.format(a, 2, -1) * 1, u = t.number.currency(u, 2, 0);
                var p = {
                    per: r,
                    bonus: t.number.currency(i, 0, 0),
                    total: t.number.currency(s, 0, 0),
                    own: t.number.currency(o, 0, 0),
                    sub: f,
                    mybonus: u
                };
                e(".templete li").eq(1).show().end().eq(0).hide().end().eq(2).hide();
                var d = '<td>\u3010</td><td><%=bonus%></td><td>\uff0d\uff08</td><td><%=bonus%></td><td>\uff0d</td><td><%=total%></td><td>\uff09\u00d7</td><td><%=per%>%</td><td>\u3011\u00d7</td><td><%=sub%>%</td><td>=</td><td><em class="red" style="font-weight:bold"><%=mybonus%></em> \u5143</td>',
					v = n.template(d);
                e(".templete li").eq(1).find(".tmp2").html(v(p)), e(".fsz20").html(u)
            }
            e(".hm-result h4").html("\u60a8\u7684\u5408\u4e70\u65b9\u6848\u5df2\u76c8\u5229\uff0c\u53d1\u8d77\u4eba\u76c8\u5229\u63d0\u6210\u4e3a" + r + "%")
        } else {
            e(".templete li").eq(2).show().end().eq(0).hide().end().eq(1).hide(), a = i * o / s, u = t.number.format(a, 2, -1) * 1, u = t.number.currency(u, 2, 0);
            var m = {
                per: r,
                bonus: t.number.currency(i, 0, 0),
                sub: f,
                mybonus: u
            },
				g = '<td>\u3010</td><td><%=bonus%></td><td>\u3011\u00d7</td><td><%=sub%>%</td><td>=</td><td><em class="red" style="font-weight:bold"><%=mybonus%></em> \u5143</td>',
				y = n.template(g);
            e(".templete li").eq(2).find(".tmp3").html(y(m)), e(".fsz20").html(u), e(".hm-result h4").html("\u60a8\u7684\u5408\u4e70\u65b9\u6848\u6ca1\u6709\u76c8\u5229\uff0c\u53d1\u8d77\u4eba\u6ca1\u6709\u76c8\u5229\u63d0\u6210!")
        }
    },

    e(function () {
        t.pages.init()
    })
})();